// *********************************************************
// classes.cpp- list of all classes required for the project. 
//
// Classes include: 
// Building, Music Player, Vehcile, PlaneEnv, Sound Player, AI, AddEnv, Map classes
// *********************************************************

/**
* BuildingN Class (similiar to the Building class)
*/
class BuildingN {

	/**
	* create a new Building and setup the model 
	*/
public: 
	void NewBuilding(int buildingId, int roofId, int size, int xpos, int zpos, int buildingTexture, int roofTexture) {

		dbRandomize(dbTimer());

		dbMakeObjectCube(buildingId, size);
		dbTextureObject(buildingId ,buildingTexture);
		dbPositionObject(buildingId, xpos, 0 , zpos);
		dbMakeObjectPlane(roofId, size, size);
		dbRotateObject(roofId, 90, 0, 0);
		//dbPositionObject(roofId, xpos, 47.6 , zpos);
		dbPositionObject(roofId, xpos, (size / 2 ) + .7 , zpos);
		dbTextureObject(roofId,roofTexture);

	}
};

/**
* Music Player- plays all music properly without anyone overlapping. 
*/
class MusicPlayer {
private:
	int musicMode;
public: 

	void loop() {
		/**
		* Main calls this loop, so that GDK can play the music. It checks the musicMode 
		* and makes it play the right music according to the appropiate action of the game. 
		*/
		// MUSIC PLAYER
		// MUSIC PLAYER
		if (musicMode ==2 && dbMusicPlaying(m_death)==0) 
		{ 
			dbStopMusic(m_combat);
			dbStopMusic(m_idle);
			changeMusic(m_death);
		}


		if (musicMode ==3 && dbMusicPlaying(m_title)==0) 
		{ 
			dbStopMusic(m_combat);
			dbStopMusic(m_idle);
			changeMusic(m_title);
		}

		if (musicMode ==1 && dbMusicPlaying(m_combat)==0) 
		{
			dbStopMusic(m_idle);
			changeMusic(m_combat);
		}
		if (musicMode ==0 && dbMusicPlaying(m_idle)==0) 
		{
			dbStopMusic(m_combat);
			changeMusic(m_idle);
		}
		musicMode=0;



	}

	/**
	* play- changes the music mode of the music object, so that it will loop a different music. 
	*/
	void play(int mode) {
		musicMode= mode;
	}

	/**
	* Returns the current music mode. 
	*/
	int getMode() {
		return musicMode;
	}

	/**
	* stops the current music, and then plays the new music of the mode changed. 
	*/
	void changeMusic(int music) {
		for (int xx=0; xx<4; xx++)
		{
			if (dbMusicLooping(xx)==1) dbStopMusic(xx);
			dbLoopMusic(music);
		}

	}


};

/**
* Vehicle Class- creates a new vehicle of different types and assigns them their life, 
* team assignment, positions them in the right place, etc. 
*/
class Vehicle {
public:


	//VEHICLE
	int NewVehicle(int team, int type) {
		/**
		* Creates the new vehicle and assigns them their team, and processes their type. 
		*/

		int x= vehicleNumStart;
		int moveable=0;
		if (type == 31 || type == 32 || type == 50 || type == 52 || type ==60 || type ==62)
			moveable= 0;

		if (type == 34 || type == 36 || type == 54 || type == 56)
			moveable= 1;

		//looks for an avialbe spot on the object array to make a new object
		for (x=vehicleNumStart; x< vehicleNumEnd+1; x++) {
			if (dbObjectExist(x) ==0) break; //x ia assigned at where the object doesn't exist
		}

		if (x< vehicleNumEnd) {
			O[x][TM]= team;

			O[x][MOVEABLE]=moveable;

			if (team==gold) {

				if (moveable ==1) {

					if (type == 34) {
						dbLoadObject("TANK1.x",x);

						//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						//dbColorObject(x, dbRGB(0,0,50));
						O[x][ML]=10;
						//money.set_money(money.money- 100); 
					}

					if (type == 36) {
						dbLoadObject("TANK1.x",x);
						//dbLoadObject("Metal Tank 3DS.3DS",x);
						//dbLoadImage("Tank Metal  Texture 4.jpg",1200);
						//dbTextureObject(x,1200);
						//dbColorObject(x, dbRGB(0,0,100));
						//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						dbScaleObject(x, 200,100,100);
						//dbScaleObject(x, 500,500,500);
						O[x][ML]=60;
						//O[x][SS]=30;
						//money.set_money(money.money- 200); 
					}


					//dbPositionObject(x, 100,0,100);
					//dbTextureObject(x,5);

					//armor -health

					O[x][L]=O[x][ML];
				}


				if (moveable ==0) {
					if (type == 31) {
						dbMakeObjectCube(x,10);
						//dbLoadObject("MilitaryBase3DS.3DS",x);
						//dbTextureObject(x,31);
						dbColorObject(x, dbRGB(0,0,25));
						//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						//money.set_money(money.money-50); 
						O[x][ML]=20;

					}


					if (type == 32) {
						dbMakeObjectCube(x,20);
						dbColorObject(x, dbRGB(0,0,100));
						//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						//money.set_money(money.money-100); 
						O[x][ML]=100;
					}
					//dbTextureObject(x,30);
					//armor -health
					O[x][L]=O[x][ML];
				}
			}


				/////////////RED TEAM
				if (team==red) {

					if (moveable ==1) {

						if (type == 34) {
							dbLoadObject("TANK5.x",x);
							//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
							//dbColorObject(x, dbRGB(100,0,0));
							O[x][ML]=10;

						}

						if (type == 36) {
							dbLoadObject("TANK5.x",x);
							dbColorObject(x, dbRGB(250,100,0));
							//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
							O[x][ML]=30;
						}

						dbScaleObject(x, 100,100,100);
						//dbPositionObject(x, 100,0,100);
						//dbTextureObject(x,5);

						//armor -health

						O[x][L]=O[x][ML];
					}
					if (moveable ==0) {


						if (type == 50) {
							dbMakeObjectSphere(x,100);
							//dbLoadObject("MilitaryBase3DS.3DS",x);
							//dbTextureObject(x,31);
							dbColorObject(x, dbRGB(50,0,0));
							//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						}


						if (type == 32) {
							dbMakeObjectSphere(x,200);
							dbColorObject(x, dbRGB(0,0,200));
							//dbLoadObject("Reduced Gold Dragon 3DS.3DS",x);
						}


						dbScaleObject(x, 10,10,10);
						//dbTextureObject(x,31);

						//armor -health
						O[x][ML]=30;
						O[x][L]=O[x][ML];
					} 

				}



				dbYRotateObject(x,180);
				dbFixObjectPivot(x);



				if (O[x][TM] == player) {
					alliesLeft++;
					dbPositionObject(x,dbRND(100)-50,0,dbRND(100)-50);
				}			

				if (O[x][TM] != player) {
					enemiesLeft++;
					dbPositionObject(x,dbRND(250)+50,0,dbRND(250)+50);
				}

				if (type == 60) {
					dbMakeObjectSphere(x,100);
					//LISA's models
					//dbLoadObject("MilitaryBase3DS.3DS",x);
					//dbLoadImage("military base texture version21.jpg", 1000);

					dbScaleObject(x, 1000,1000,1000);

					dbTextureObject(x,1000);
					O[x][ML]=300;
					O[x][L]=O[x][ML];
					dbPositionObject(x,0,0,-50);		
				}

				if (type == 62) {
					//LISA's models
					dbMakeObjectSphere(x,100);
					/*dbLoadObject("Reduced Town Center 3DS.3DS",x);
					dbLoadImage("TownCenterTextureVersion21.jpg", 1000);*/
					dbScaleObject(x, 1000,1000,1000);

					dbTextureObject(x,1000);
					O[x][ML]=300;
					O[x][L]=O[x][ML];
					dbPositionObject(x,200,0,200);		
				}


				//person
				O[x][PER]= dbRND(1);

				O[x][DX]= dbObjectPositionX(x);
				O[x][DZ]= dbObjectPositionZ(x);

				O[x][AR]=100; //default attack range

				return x;
			}
			else 
				return 0;
		}


		// IS VEHICLE?
		int isVehcile(int id) {
			/**
			* Checks to see if this object number is a vehicle, if not, return 0. 
			*/
			if (id >= vehicleNumStart && id <= vehicleNumEnd)
				return 1;
			else 
				return 0;

		}


	};



	/**
	* planeEnv deals with the creation of enviroment decorations such as planes flying around. 
	* Very useful effect for game play. 
	* It also randomly picks 2 different types of planes to display. 
	*/
	class planeEnv {
	public:

		//TOUR PLANE
		void newTourPlane(int id) {
			/**
			* creates new plane with an object ID. 
			*/

			int pick=dbRND(1);

			if (pick==0) {
				dbLoadObject("AIRPL011.x",id); 
				dbScaleObject(id,300,300,300);
			}
			if (pick==1) {
				dbLoadObject("AIRPL016.x",id);
				dbYRotateObject(id,180);
				dbFixObjectPivot(id);

			}

			dbRandomize(dbTimer());
			dbYRotateObject(id,dbRND(360));
			O[id][SL]= dbRND(30)*.1+3;
			O[id][RL]= (dbRND(5)*.1 +.05) * (1-dbRND(2)) +.01;

			dbPositionObject(id,dbRND(1000)-500,dbRND(20)+50,dbRND(1000)-500);

		}


	};






	/**
	* Sound player class is responsible for sounds to not overlap severly. 
	* 
	*/
	class SoundPlayer {
	private:
		int soundMode;
	public: 

		/**
		* Main calls this loop to get GDK to play the sounds. 
		* 
		*/
		void loop() {

			if (soundMode ==2 && dbSoundPlaying(m_death)==0) 
			{ 
				dbStopSound(m_combat);
				dbStopSound(m_idle);
				changeSound(m_death);
			}


			if (soundMode ==3 && dbSoundPlaying(m_title)==0) 
			{ 
				dbStopSound(m_combat);
				dbStopSound(m_idle);
				changeSound(m_title);
			}

			if (soundMode ==1 && dbSoundPlaying(m_combat)==0) 
			{
				dbStopSound(m_idle);
				changeSound(m_combat);
			}
			if (soundMode ==0 && dbSoundPlaying(m_idle)==0) 
			{
				dbStopSound(m_combat);
				changeSound(m_idle);
			}
			soundMode=0;
		}
		/**
		* Changes the sound mode.   
		* 
		*/

		void play(int mode) {
			soundMode= mode;
		}

		/**
		* Returns the soundmode
		* 
		*/
		int getMode() {
			return soundMode;
		}

		/**
		* stops current sound and plays new sound. 
		* 
		*/
		void changeSound(int sound) {
			for (int xx=0; xx<4; xx++)
			{
				if (dbSoundLooping(xx)==1) dbStopSound(xx);
				dbLoopSound(sound);
			}

		}


	};



	/**
	*AI class controls the AI's behaviors. 
	* 
	*/
	class AI {

	public: 

		/**
		*Creates a new AI object to control the AI. 
		* 
		*/
		void newAI() {
			int xx=6; 
			int loopout2=0;
			for (int yy= vehicleNumStart; yy< vehicleNumEnd && loopout2==0; yy++) {
				if (O[xx][TM] != O[yy][TM]
				&& O[yy][L]>=0
					&& dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )<5
					&& O[xx][ET]==0
					&& withinRange(xx,yy,200) ==1) {


						if ((withinRange(xx,yy,100) ==0)
							&& (withinRange(xx,yy,150) ==1)
							&& O[xx][ET]==0)


							//dbMoveObject(xx,.2);
							//O[xx][ET]=yy;
							//dbPointObject(xx, O[yy][X],0,O[yy][Z]);





							dbPointObject(xx, O[yy][X],0,O[yy][Z]);
						//O[xx][FW]=fire;

						loopout2=1;
						//else changeMusic(7);

				}

			}



		}
	};


	/**
	* Creates a new enviroment for the game. 
	* 
	*/
	class AddEnv {

	public: 

		/**
		* Adds the environment. 
		* 
		*/
		void addEnv() {

			dbLoadImage(
				"building1.bmp", 30005);

			dbLoadImage(
				"building1roof.bmp", 30007);

			dbLoadImage(
				"building2.bmp", 30008);

			dbLoadImage(
				"building2roof.bmp", 30009);

			dbLoadImage(
				"building300.bmp", 30010);

			dbLoadImage(
				"building300roof.bmp", 30011);

			dbLoadImage(
				"building4.bmp", 30012);

			dbLoadImage(
				"building4roof.bmp", 30013);

			int builTex[6];

			builTex[0] = 30005;

			builTex[1] = 30005;

			builTex[2] = 30008;

			builTex[3] = 30010;

			builTex[4] = 30012;

			builTex[5] = 30012;

			int roofTex[6];

			roofTex[0] = 30007;

			roofTex[1] = 30007;

			roofTex[2] = 30009;

			roofTex[3] = 30011;

			roofTex[4] = 30013;

			roofTex[5] = 30013;

			//Make the Ground

			dbLoadImage("ground.jpg",10000);

			dbLoadImage("road.bmp", 3);

			dbMakeMatrix(1,3000,3000,10,10);

			dbRandomizeMatrix(1,3);

			//dbPositionMatrix(30000,-500,-10,-500); //DELETE -10 WHEn FINISH

			dbPositionMatrix(1,-500,-10,-500);

			dbPrepareMatrixTexture(1,10000,1,1);


		}
	};


	
	/**
	* Create the map of the place. 
	* 
	*/
	class Map {
		
	public: 

		void newMap() {
		/**
		* Creates the new map matrix and textures it properly. 
		* 
		*/

		dbLoadImage("ground.jpg",10000);
	
		dbLoadImage("road.bmp", 3);
	
		dbMakeMatrix(1,3000,3000,10,10);
	
		dbRandomizeMatrix(1,3);
	
		//dbPositionMatrix(30000,-500,-10,-500); //DELETE -10 WHEn FINISH
	
		dbPositionMatrix(1,-500,-10,-500);
	
		dbPrepareMatrixTexture(1,10000,1,1);
		
		
	   }
	};
	
	
	

